package com.lazy.utils.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * 数据库源配置
 * @author lcx
 *
 */
@Configuration
@ConditionalOnProperty("mysql.datasource.master.url")
public class DataSourceConfiguration {

	private static Logger logger = LoggerFactory.getLogger(DataSourceConfiguration.class);
	
	@Value("${mysql.datasource.type}")
	private Class<? extends DataSource> dataSourceType;

	@Value("mysql.datasource.slowSqlMillis")
    private String slowSqlMillis;

	@Value("mysql.datasource.logSlowSql")
    private String logSlowSql;

	private final MasterDataSourceDruidConfig masterDataSourceDruidConfig;

    private final SlaveFirstDataSourceDruidConfig slaveFirstDataSourceDruidConfig;

    private final SlaveSecondDataSourceDruidConfig slaveSecondDataSourceDruidConfig;

    private final SlaveThirdDataSourceDruidConfig slaveThirdDataSourceDruidConfig;

    @Autowired
    public DataSourceConfiguration(MasterDataSourceDruidConfig masterDataSourceDruidConfig, SlaveFirstDataSourceDruidConfig slaveFirstDataSourceDruidConfig, SlaveSecondDataSourceDruidConfig slaveSecondDataSourceDruidConfig, SlaveThirdDataSourceDruidConfig slaveThirdDataSourceDruidConfig) {
        this.masterDataSourceDruidConfig = masterDataSourceDruidConfig;
        this.slaveFirstDataSourceDruidConfig = slaveFirstDataSourceDruidConfig;
        this.slaveSecondDataSourceDruidConfig = slaveSecondDataSourceDruidConfig;
        this.slaveThirdDataSourceDruidConfig = slaveThirdDataSourceDruidConfig;
    }

    /**
	 * 写库 数据源配置
	 * @return
	 */
	@Bean(name = "masterDataSource")
    @Primary
    public DataSource masterDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        DruidConfig.build(druidDataSource, masterDataSourceDruidConfig);
        logger.info("-------------------- masterDataSource init ---------------------");
        return druidDataSource;
    }
	
	/**
     * 有多少个从库就要配置多少个
     * @return
     */
    @Bean(name = "slaveDataSource1")
    @ConditionalOnProperty("mysql.datasource.slave01.url")
    public DataSource slaveDataSource1() {
        DruidDataSource druidDataSource = new DruidDataSource();
        DruidConfig.build(druidDataSource, slaveFirstDataSourceDruidConfig);
        logger.info("-------------------- slaveDataSource1 init ---------------------");
        return druidDataSource;
    }

    @Bean(name = "slaveDataSource2")
    @ConditionalOnProperty("mysql.datasource.slave02.url")
    public DataSource slaveDataSource2() {
        DruidDataSource druidDataSource = new DruidDataSource();
        DruidConfig.build(druidDataSource, slaveSecondDataSourceDruidConfig);
        logger.info("-------------------- slaveDataSource2 init ---------------------");
        return druidDataSource;
    }

    @Bean(name = "slaveDataSource3")
    @ConditionalOnProperty("mysql.datasource.slave03.url")
    public DataSource slaveDataSource3() {
        DruidDataSource druidDataSource = new DruidDataSource();
        DruidConfig.build(druidDataSource, slaveThirdDataSourceDruidConfig);
        logger.info("-------------------- slaveDataSource3 init ---------------------");
        return druidDataSource;
    }

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", "new_loan");
        reg.addInitParameter("loginPassword", "new_loan");
        reg.addInitParameter("resetEnable", "false");
        return reg;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        return filterRegistrationBean;
    }
}
